package com.scpii.universal.util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class UtilTool {
	private static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	/**
	 * 将String转换成Integer
	 * 
	 * @param strs
	 * @return
	 */
	public static Integer StringToInteger(String strs) {
		if (strs != null && strs.trim().length()>0) {
			try {
				return Integer.parseInt(strs);
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}
		}
		return 0;
	}

	/**
	 * 将Object转换成Integer
	 * 
	 * @param obj
	 * @return
	 */
	public static Integer ObjectToInteger(Object obj) {
		if (obj != null) {
			try {
				return (Integer) obj;
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}
		}
		return 0;
	}

	/**
	 * 日期转换成字符串
	 */
	public static String dateToString(Date date) {
		if (date != null) {
			return dateFormat.format(date);
		}
		return null;
	}

	/**
	 * 字符串转换成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDate(String dateStr) {
		if (dateStr != null) {
			try {
				if (dateStr.length() == 10) {
					dateFormat = new SimpleDateFormat("yyyy-MM-dd");
					Date date = dateFormat.parse(dateStr);
					return date;
				} else if (dateStr.length() > 10) {
					Date date = dateFormat.parse(dateStr);
					return date;
				}
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}

	/**
	 * 用于删除文件
	 * 
	 * @param path
	 * @throws Exception
	 */
	public static void deleteFile(String path) {
		if (path != null) {
			File file = new File(path);
			boolean bool = file.delete();
			System.out.println(bool);
		}
	}
	
	/**
	 * 提取字符串中的包含的数字出来
	 * @param strs
	 * @return
	 * @author 甘建新
	 * 2012-9-20下午11:20:37
	 */
	public static Integer[] getNumsFromStr(String strs){
		Integer[] inums = new Integer[]{};
		String numsStr ="";
		if (strs!=null && strs.trim().length()>0) {
			int strsLength = strs.length();
			List<Character> charList = new ArrayList<Character>();
			for (int i = 0; i < strsLength; i++) {
				char nowchr = strs.charAt(i);
				char beforeChr=100;
				char nextChr=100;
				//取当前的前一个和下一个字符 
				if (i>0 && (i+1)<=(strsLength-1)) {
					beforeChr = strs.charAt(i-1);
					nextChr = strs.charAt(i+1);
				} else {
					System.out.println("dddd");
					beforeChr = '0';
					if ((i+1)==strsLength || (i+1)>strsLength) {
						nextChr='0';
					}else {
						nextChr = strs.charAt(i+1);
					}
				}
				
				if (charIsIn0And9(nowchr)) {
					//当前字符的前一个字符不是以0开始的数字，并且下一个字符不为数字
					if (!charIsIn0And9(nextChr)) {
						charList.add(nowchr);
						charList.add("-".charAt(0));
					} else if((!charIsIn0And9(beforeChr) && nowchr=='0')) {
						//判断数字第一位不能为0，为0就跳过继续循环
						continue;
					}else if(nowchr=='0' && !charIsIn0And9(nextChr)){
						continue;
					}else {
						charList.add(nowchr);
					}
				}
			}
			
			if (charList!=null && charList.size()>0) {
				for (Character character : charList) {
					numsStr = numsStr+character.toString();
				}
			}
			
			//转换到INTEGER数组中
			String[] strss = numsStr.split("-");
			if (strss!=null && strss.length>0) {
				int length = strss.length;
				inums = new Integer[strss.length];
				for (int i = 0; i < length; i++) {
					try {
						int num = Integer.parseInt(strss[i]);
						inums[i]=num;
					} catch (Exception e) {
						inums[i]=0;
					}
				}
				//对数组进行排序
				Arrays.sort(inums);
			}
		}
		return inums;
	}

	/**
	 * 判断字符是否为数字
	 * @param chr
	 * @return
	 * @author 甘建新
	 * 2012-9-20下午11:33:17
	 */
	private static boolean charIsIn0And9(char chr){
		if (chr>='0' && chr<='9') {
			return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		Integer[] nums = getNumsFromStr("1212,00300aadsdd3234234a10000da0000de22330");
		System.out.println("===========");
		for (Integer integer : nums) {
			System.out.println(integer);
		}
	}
}
